package com.yzs.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yzs.common.bean.R;
import com.yzs.entity.Admin;
import com.yzs.entity.Session;
import com.yzs.mapper.AdminMapper;
import com.yzs.mapper.SessionMapper;
import com.yzs.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/session")
public class SessionController {

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private SessionMapper sessionMapper;
    @Autowired
    JwtUtil JwtUtil;

    /**
     * 已建立会话
     */
    @RequestMapping("/already")
    public R sessionListAlready(@RequestParam Integer id){
        QueryWrapper<Session> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",id);
        List<Session> sessionList = sessionMapper.selectList(wrapper);
        return  R.ok(sessionList);
    }

    // 可建立会话的用户
    @RequestMapping("/not")
    public R sessionListNot(@RequestParam Integer id){
        QueryWrapper<Session> wrapper = new QueryWrapper<>();
        wrapper.select("to_user_id").eq("user_id",id);
        List<Session> list = sessionMapper.selectList(wrapper);
        ArrayList<Integer> toUserIdList = new ArrayList<>();
        list.stream().forEach((session)->toUserIdList.add(session.getToUserId()));
        toUserIdList.add(id);
        // 获取用户数据
        QueryWrapper<Admin> adminWrapper = new QueryWrapper<>();
        adminWrapper.select("id","name").notIn("id",toUserIdList);
        List<Admin> cloudList = adminMapper.selectList(adminWrapper);
        return R.ok(cloudList);
    }

    // 创建会话
    @RequestMapping("/createSession")
    public R createSession(@RequestParam Integer id,@RequestParam Integer toUserId,@RequestParam String toUserName){
        Session session = new Session();
        session.setUserId(id);
        session.setUnreadCount(0);
        session.setListName(toUserName);
        session.setToUserId(toUserId);
        sessionMapper.insert(session);
        // 判断对方和我建立会话没有？ 没有也要建立
        QueryWrapper<Session> sessionWrapper = new QueryWrapper<>();
        sessionWrapper.eq("user_id",toUserId).eq("to_user_id",id);
        Session s = sessionMapper.selectOne(sessionWrapper);
        if (s == null){
            QueryWrapper<Admin> adminWrapper = new QueryWrapper<>();
            adminWrapper.select("id","name").eq("id",id);
            Admin admin = adminMapper.selectOne(adminWrapper);
            session.setUserId(toUserId);
            session.setToUserId(id);
            session.setListName(admin.getName());
            session.setId(session.getId()+1);
            sessionMapper.insert(session);
        }
        return R.ok();
    }

    // 删除会话
    @RequestMapping("/delSession")
    public R delSession(@RequestParam Integer sessionId){
        sessionMapper.deleteById(sessionId);
        return R.ok();
    }

    @RequestMapping("/chatVerifyToken")
    public R verifyToken(@RequestParam String token){
        Integer id = JwtUtil.verifyToken(token);
        if(id > 0){
            Admin admin = adminMapper.selectById(id);
            return R.ok(admin);
        }
        return R.error("请登录再试！");
    }

}
